import requests
from bs4 import BeautifulSoup
from contentforge.utils.logger import logger

def fetch_content_from_url(url: str) -> str:
    """从链接中获取网页的文本和图片内容，保持原有顺序"""
    try:
        headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3",
            "Accept-Language": "en-US,en;q=0.9",
            "Accept-Encoding": "gzip, deflate, br",
            "Connection": "keep-alive"
        }
        
        response = requests.get(url.strip(), headers=headers)
        response.raise_for_status()
        soup = BeautifulSoup(response.text, 'html.parser')
        
        # 提取正文内容
        body = soup.find('body')
        if not body:
            raise ValueError("未找到正文内容")
        
        content = []
        
        # 遍历body的子元素，保留文本和图片的顺序
        for element in body.descendants:
            if element.name == 'img' and 'src' in element.attrs:
                content.append(f"![Image]({element['src']})")
            elif element.string and element.string.strip():
                content.append(element.string.strip())
        
        # 将内容组合成Markdown格式
        return "\n\n".join(content)
    
    except Exception as e:
        logger.error(f"获取链接内容失败: {str(e)}")
        return "" 